
package foundation;

import java.sql.*;


public class FConnection {

	private static FConnection instance = null;
	
	//Database connection settings
	private String db="TaxaTracer";
	private String ip = "localhost";
	private int port = 3306; 



	/**
	 * Connection fås vha. singleton
	 */
	public static FConnection getConnection(){
		if (instance == null) 
			synchronized (FConnection.class) { //tråd sikker
				if (instance == null) instance = new FConnection();
			}
		return instance;
	}

	public Connection dbConnect(String user, String pass){

		Connection con=null;

		try {

			//Register the JDBC driver for MySQL.
			Class.forName("com.mysql.jdbc.Driver");

			//Define URL of database server, here
			// database on the localhost
			// with the default port number 3306.
			String url ="jdbc:mysql://"+ip+":3306/"+db;

			//Get a connection to the database for a
			// user.
			con = DriverManager.getConnection(url,user, pass);

			//Display URL and connection information
			System.out.println("URL: " + url);
			System.out.println("Connection: " + con);

		}catch( Exception e ) {
			e.printStackTrace();}
		//end catch
		return con;
	}
	
	/**
	 * dbCloseCon is closing the connection
	 * send the connection object her, to close connection
	 * 
	 * @param con - the Connection object to shutdown
	 */
	
	public void dbCloseCon(Connection con){

		// Connection con = dbGetConnection();
		try {
			con.close();//Close connection
			System.out.println("Connection Closed");
		} catch( Exception e ) {
			e.printStackTrace();
		}//end catch

	}	

}
